<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主体 -->
        <el-tree
          default-expand-all
          :data="data"
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item

                          @click.native="hDel(data.id,data)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹层子组件-->
      <el-dialog
        title="添加或编辑"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <addor-edit
          v-if="showDialog"
          :id="curId"
          :is-edit="isEdit"
          :origin-list="originList"
          @close="hClose"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/tranListToTreeData'
import AddorEdit from './deptDialog.vue'
export default {
  name: 'Departments',
  components: { AddorEdit },
  props: {
  },
  data() {
    return {
      data: [],
      showDialog: false, // 是否显示弹层
      curId: '',
      isEdit: false, // 是否处于编辑状态
      originList: []
    }
  },
  created() {
    // 打开页面获取基本数据
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      const res = await getDepartments()
      console.log(res)
      res.data.depts.shift()
      this.originList = res.data.depts.map(item => { return { id: item.id, pid: item.pid, name: item.name, code: item.code } })
      this.data = tranListToTreeData(res.data.depts)
      console.log(this.data)
    },
    handleNodeClick(data) {
      console.log(data)
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },

    // 用户点击了编辑
    hEdit(id) {
      // 1. 弹层出来
      this.showDialog = true
      // 2. 表单中要显示详情数据
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      this.isEdit = true
    },

    // onSubmit() {
    //   // todo: 表单校验
    //   this.doAdd()
    // },
    // 收到子组件中的success事件

    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartments()
    },
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hDel(id, data) {
      console.log(data)
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    },
    hClose() {
      this.showDialog = false
    }
  }
}
</script>
